package Convertisseur;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import DataStorage.CodageCheminDAOHibernate;
import DataStorage.EchantillonDAOHibernate;
import DataStorage.InformationLigneDAOHibernate;

public class Verification {

	private String chemin;
	private String N_serie;
	private Map<String, String> map_OP_Chemin = new HashMap<String, String>();
	private Map<String, String> map_OP_Bras = new HashMap<String, String>();
	//essieu
	@SuppressWarnings("rawtypes")
	protected static List<List> list_ToutOPChemin = new ArrayList<List>();
	protected static List<String> listCodageChemin = new ArrayList<String>();
	//bras
	@SuppressWarnings("rawtypes")
	protected static List<List> list_ToutOPBras = new ArrayList<List>();
	protected static List<String> listCodageBras = new ArrayList<String>();


	private CodageCheminDAOHibernate CodageCheminDAO = new CodageCheminDAOHibernate();
	private String site;
	private String N_ligne;
	private String Annee;
	protected static long InformationLigne_id;
	private String critere_name;
	private String Organe;
	private String Diversite;
	private String filePath = "";
	private String bras;

	
	
	// --------------------------------------------------------------------------------------
	// Data interface
	// --------------------------------------------------------------------------------------

	
	public String getFilePath() {
		return filePath;
	}

	public String getOrgane() {
		return Organe;
	}

	public void setOrgane(String organe) {
		Organe = organe;
	}

	public String getDiversite() {
		return Diversite;
	}

	public void setDiversite(String diversite) {
		Diversite = diversite;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public String getN_ligne() {
		return N_ligne;
	}

	public void setN_ligne(String n_ligne) {
		N_ligne = n_ligne;
	}

	public String getAnnee() {
		return Annee;
	}

	public void setAnnee(String annee) {
		Annee = annee;
	}

	// --------------------------------------------------------------------------------------
	// Data file Macro
	// --------------------------------------------------------------------------------------

	public String getChemin() {
		return chemin;
	}

	public void setChemin(String chemin) {
		this.chemin = chemin;
	}
	
	
	public String getN_serie() {
		return N_serie;
	}

	public void setN_serie(String n_serie) {
		N_serie = n_serie;
	}

	public String getBras() {
		return bras;
	}

	public void setBras(String bras) {
		this.bras = bras;
	}

	public String getCritere_name(Sheet sheet) {
		// reception values cells
		Cell cellK1001 = sheet.getCell("A1"); // Organe(nom_Essieu):X91_Essieu

		String piece = cellK1001.getContents().toString();
		critere_name = piece.substring(piece.lastIndexOf('\\') + 1,
				piece.lastIndexOf(".pce"));

		return critere_name;

	}
	
	public long getInformationLigne_id() {
		InformationLigneDAOHibernate informationLigneDAO = new InformationLigneDAOHibernate();

		InformationLigne_id = informationLigneDAO.getInformationLigne_ID(
				this.getOrgane(), this.getDiversite(),
				this.getSite(), this.getN_ligne(), this.getAnnee());
		return InformationLigne_id;
	}
	
	//-------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4; 
	//	chemin: 330, 110, 330, 110
	//
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//	return map<OP, outil>
	//
	//  essieu 1: <130,3> <140,3> <170,0>
	//  essieu 2: <130,3> <140,3> <170,0>
	//  essieu 3: <130,3> <140,3> <170,0>
	//  essieu 4: <130,3> <140,3> <170,0>
	//-------------------------------------------------------
	public Map<String, String> getMap_OP_Chemin(Sheet sheet) {
		System.out.println("[getMap_OP]");

		String Chemin = this.getChemin(sheet);
		char[] arr_chemin = Chemin.toCharArray();
		// listCodageChemin =
		// CodageCheminDAO.getListOP(this.getInformationLigne_id(sheet));
		int sizeCodageChemin = listCodageChemin.size();

		if (arr_chemin.length == sizeCodageChemin) {
			for (int i = 0; i < sizeCodageChemin; i++) {
				// ex:Map =(105,1)
				map_OP_Chemin.put(listCodageChemin.get(i), "" + arr_chemin[i]);
			}
		} else {
			System.out
					.println("length of chemin Macro != length of CodageChemin");
		}
		return map_OP_Chemin;
	}
	
	//-------------------------------------------------------
	//	example X61 
	//	nombre essieu:  4; 
	//	nombre Bras:  4;
	//	chemin d'essieu: 579653, 579654, 579655, 579656
	//	chemin de bras: 11, 22, 33, 44
	//
	//	OP d'essieu: 			[ 	110, 	|	130,  |	  160 	| 170 	|  180 	|  190 	]
	//	chemin d'essieu:		[ 0,1,2,3,4	|	0,1,2 |	0,1,2,3 | 0,1,2	| 0,1,2 | 0,1,2	]
	//  OP de bras: 			[ 	100 	| 	110		]
	//  chemin de bras: 		[ 0,1,2,3,4 | 0,1,2,3,4 ]
	//	return map<OP, outil de bras>
	//
	//  Bras 1: <100,1> <110,1>
	//  Bras 2: <100,2> <110,2> 
	//  Bras 3: <100,3> <110,3>
	//  Bras 4: <100,4> <110,4>
	//-------------------------------------------------------
	
	public Map<String, String> getMap_OP_Bras(Sheet sheet) {
		System.out.println("[getMap_OP_Bras]");

		String Chemin = this.getChemin(sheet);
		EchantillonDAOHibernate echantillonDAO = new EchantillonDAOHibernate();
		String chemin_Supplementaire = echantillonDAO.getChemin_SousEnsembleSupplementaire(InformationLigne_id);
		
		String[] arr_chemin_supp = null;
		if(!chemin_Supplementaire.equals("")||!chemin_Supplementaire.equals(null)){
			arr_chemin_supp = chemin_Supplementaire.split(",");
		}
		if(!arr_chemin_supp.equals(null)){
			for (int i = 0; i < arr_chemin_supp.length; i++) { //
				map_OP_Bras.put(arr_chemin_supp[i], "" + Chemin.charAt(i));
			}
		}
		return map_OP_Bras;
	}
	
	//-----------------------------------------------------------------------------
	//  OP de bras: 			[ 	100 	| 	110		]
	//  chemin de bras: 		[ 0,1,2,3,4 | 0,1,2,3,4 ]
	// 	return list : 			[100 , 110]
	//-----------------------------------------------------------------------------
	public List<String> getListCodageChemin_Bras(){
		List<String> ListCodageCheminBras = new ArrayList<String>();
		EchantillonDAOHibernate echantillonDAO = new EchantillonDAOHibernate();
		String chemin_Supplementaire = echantillonDAO.getChemin_SousEnsembleSupplementaire(InformationLigne_id);
		
		String[] arr_chemin_supp = null;
		if(!chemin_Supplementaire.equals("")||!chemin_Supplementaire.equals(null)){
			arr_chemin_supp = chemin_Supplementaire.split(",");
		}
		if(!arr_chemin_supp.equals(null)){
			for (int i = 0; i < arr_chemin_supp.length; i++) { //
				ListCodageCheminBras.add(arr_chemin_supp[i]);
			}
		}
		return ListCodageCheminBras;
	}
	
	//-----------------------------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4; 
	//	chemin: 330, 110, 330, 110
	//
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//
	// Essieu: 	list_ToutOPChemin:[[3, 3, 0], [1, 1, 0], [1, 1, 0], [3, 3, 0]]
	//---------------------------------------------------------------------------
	// 
	//---------------------------------------------------------------------------
	//	example X61 
	//	nombre essieu:  4; 
	//	nombre Bras:  4;
	//	chemin d'essieu: 579653, 579654, 579655, 579656
	//	chemin de bras: 11, 22, 33, 44
	//
	//	OP d'essieu: 			[ 	110, 	|	130,  |	  160 	| 170 	|  180 	|  190 	]
	//	chemin d'essieu:		[ 0,1,2,3,4	|	0,1,2 |	0,1,2,3 | 0,1,2	| 0,1,2 | 0,1,2	]
	//  OP de bras: 			[ 	100 	| 	110		]
	//  chemin de bras: 		[ 0,1,2,3,4 | 0,1,2,3,4 ]
	//
	//	Essieu: 	list_ToutOPChemin:[[3, 2, 3, 2, 1, 2], [4, 2, 1, 2, 1, 2], [2, 1, 3, 1, 2, 1], [1, 1, 2, 1, 2, 1]]
	//	Bras:		list_ToutOPChemin:[[3, 3], [1, 1], [2, 2], [4, 4]]
	//--------------------------------------------------------------------------

	public void setList_mapOPChemin(String filePath) {
		System.out.println("[setList_mapOPChemin]");

		File fileExcel = new File(filePath);
		try {
			Workbook book = Workbook.getWorkbook(fileExcel);

			int nbSheet = book.getNumberOfSheets();
			for (int i = 0; i < nbSheet; i++) {
				Sheet sheet = book.getSheet(i);
				
				String identificationPiece = this.getIdentificationPiece(sheet);
				//essieu
				if(identificationPiece.contains("ch")){
					listCodageChemin = CodageCheminDAO.getListOP(this
							.getInformationLigne_id());
					List<String> outils = new ArrayList<String>();				
					Map<String, String> map = getMap_OP_Chemin(sheet);
					for (String op : listCodageChemin) {
						outils.add(map.get(op));
					}

					list_ToutOPChemin.add(outils);
					System.out.println("list_ToutOPChemin essieu: "+list_ToutOPChemin);
				}
				
				//bras  	list_ToutOPBras  listCodageBras
				if(identificationPiece.contains("bras")){
					listCodageBras = getListCodageChemin_Bras();

					List<String> outils = new ArrayList<String>();				
					Map<String, String> map = getMap_OP_Bras(sheet);
					for (String op : listCodageBras) {
						outils.add(map.get(op));
					}

					list_ToutOPBras.add(outils);
					System.out.println("list_ToutOPBras bras: "+list_ToutOPBras);
				}	
				
				
			}
			book.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getIdentificationPiece(Sheet sheet){
		System.out.println("[getIdentificationPiece]");

		// init
		ExcelPublicMethod.setSheet(sheet);
		ExcelPublicMethod.setMacroColumn_CorrespondsTo_ExcelColumn();
		ExcelPublicMethod.setIndexCell_Number();
		
		// IDENTIFICATION DE LA PIECE 
		Cell cellIdentificationPiece = sheet.getCell("A2");
		String contentsIdentificationPiece = cellIdentificationPiece.getContents()
				.toString();
	
		if (contentsIdentificationPiece.contains("o")) {
			contentsIdentificationPiece = contentsIdentificationPiece.substring(
					contentsIdentificationPiece.lastIndexOf(':') + 2,
					contentsIdentificationPiece.lastIndexOf('o') - 1);
		} else {
			contentsIdentificationPiece = contentsIdentificationPiece.substring(
					contentsIdentificationPiece.lastIndexOf(':') + 2,
					contentsIdentificationPiece.length());
		}
		contentsIdentificationPiece = contentsIdentificationPiece.toLowerCase();
		contentsIdentificationPiece = contentsIdentificationPiece.replaceAll("	", "");
		contentsIdentificationPiece = contentsIdentificationPiece.replaceAll(" ", "");
		
		
		return contentsIdentificationPiece;
	}
	
	public String getChemin(Sheet sheet) {
		String IdentificationPiece = this.getIdentificationPiece(sheet);
		// chemin d'essieu
		if (IdentificationPiece.contains("ch")
				&& !IdentificationPiece.contains("bras")) {
			// Number_Piece and Path_Coding
			chemin = IdentificationPiece.substring(
					IdentificationPiece.lastIndexOf("ch"),
					IdentificationPiece.length());
			chemin = chemin.replaceAll("[a-zA-Z]", "");
		}
		// chemin de bras
		if (!IdentificationPiece.contains("ch")
				&& IdentificationPiece.contains("bras")) {
			String critere_name = getCritere_name(sheet);
			if (critere_name.equals("X61_Essieu")) {
				String bras = this.getBras(sheet);
				if (bras.equals("A21")) {
					chemin = "11";
				}
				if (bras.equals("A22")) {
					chemin = "22";
				}
				if (bras.equals("B21")) {
					chemin = "33";
				}
				if (bras.equals("B22")) {
					chemin = "44";
				}
			}
		}
		return chemin;
	}

	public String getN_serie(Sheet sheet) {
		System.out.println("[getN_serie]");

		String essieuEtChemin = this.getIdentificationPiece(sheet);
		// Number_Piece and Path_Coding
		N_serie = essieuEtChemin.substring(0, essieuEtChemin.lastIndexOf("ch"));
		return N_serie;
	}
	
	public String getBras(Sheet sheet){
		System.out.println("[getBras]");

		String brasString = this.getIdentificationPiece(sheet);
		if(brasString.contains("bras")){
			this.bras=brasString.substring(brasString.lastIndexOf("bras")+4, brasString.length());
		}
		this.bras = this.bras.toUpperCase();
		return this.bras;
	}

	// --------------------------------------------------------------------------------------
	// Data DB
	// --------------------------------------------------------------------------------------

	public List<String> getCheminPiecePrincipaleByDB() {
		System.out.println("[getCheminPiecePrincipaleByDB]");

		List<String> list = new ArrayList<String>();
		EchantillonDAOHibernate EchantillonDAO = new EchantillonDAOHibernate();
		String cheminPiecePrincipale = EchantillonDAO
				.getCheminPiecePrincipale(InformationLigne_id);
		String[] table = cheminPiecePrincipale.split(",");
		for (int i = 0; i < table.length; i++) {
			list.add(RegexString.filterRestNumber(table[i]));
		}

		return list;
	}



	@SuppressWarnings("static-access")
	public void executer(String filePath) {
		System.out.println("[executer]");
		// clear tous les variable static
		list_ToutOPChemin.clear();
		listCodageChemin.clear();
		list_ToutOPBras.clear();
		listCodageBras.clear();
				
		
		setList_mapOPChemin(filePath);
		File fileExcel = new File(filePath);
		
		try {
			VerifierMetaReglesDeCoherenceEchantillon verifierMata = new VerifierMetaReglesDeCoherenceEchantillon();
			verifierMata.verifierMetaRegle();
			
			Workbook book = Workbook.getWorkbook(fileExcel);
			VerifierCheminsEchantillon verifierChemins = new VerifierCheminsEchantillon();
			VerifierListCoupeEchantillonAvecListDefinie verifierCoupe = new VerifierListCoupeEchantillonAvecListDefinie();
			
			int nbSheet = book.getNumberOfSheets();
			//clear variable static
			verifierCoupe.getMap_coupe_message().clear();
			verifierChemins.getMap_essieu_messageChemin().clear();
			
			for (int i = 0; i < nbSheet; i++) {
				Sheet sheet = book.getSheet(i);
				String identificationPiece = this.getIdentificationPiece(sheet);
				//essieu
				if(!identificationPiece.contains("bras")){
					verifierChemins.verifieCheminEssieu(sheet);
					
				}
				verifierCoupe.VerifierListCoupeEchantillon(sheet);

			}
			
			book.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		
		Verification verification = new Verification();

		verification.setSite("Le Mans");
		verification.setN_ligne("1");
		verification.setAnnee("2012");
		verification.setOrgane("X61");
		verification.setDiversite("Essieu");
		
		
		verification.executer("temp\\fileTemp.xls");
		
		
	}

}
